<!--
  移动签批->公文详情->发文->填写意见
-->
<template>
  <view>
    <van-popup v-model="showCompo" closeable round position="bottom" :style="{ height: '80%' }"
               @click-close-icon="toClose" @click-overlay="toClose">
      <van-row type="flex" justify="center">
        <van-col offset="3" span="8">
          <van-cell value="填写意见" />
        </van-col>
      </van-row>
      <van-form @submit="onSubmit">
        <van-cell is-link title="意见类型" v-model="yjlx" @click="showyjlx = true" />
        <van-action-sheet ref="yjlx" v-model="showyjlx" :actions="yjlxList" cancel-text="取消"
                          close-on-click-action @select="yjlxChange" />


        <van-cell is-link title="批示方式" v-model="psfs" @click="showpsfs = true" />
        <van-action-sheet ref="psfs" v-model="showpsfs" :actions="psfsList" cancel-text="取消"
                          close-on-click-action @select="psfsChange" />

        <template v-if="showOpinion">
          <van-cell title="填写意见" :border="border"></van-cell>
          <van-field
              v-model="message"
              rows="2"
              autosize
              type="textarea"
              placeholder="请输入"
              clearable
          />
        </template>

        <view class="css">
          <van-row type="flex" justify="space-around">
            <van-col span="10">
              <van-button type="info" round plain hairline block @click.prevent="toClose">取消</van-button>
            </van-col>
            <van-col span="10">
              <van-button type="info" round block  native-type="onSubmit">确定</van-button>
            </van-col>
          </van-row>
        </view>

      </van-form>
    </van-popup>
  </view>
</template>

<script>
export default {
  components: {},
  props: {
    showRecords: {
      type: Boolean,
      default: false
    },
    paramObject:{
      type: Object
    }
  },
  data() {
    return {
      showCompo:false,//组件是否显示
      autosize: {
        maxHeight: 50,
        minHeight: 30
      },
      showOpinion:false,//显示填写意见框
      border: false,
      yjlx: '请选择意见类型',
      psfs: '请选择批示方式',
      showyjlx: false,
      showpsfs: false,
      //意见类型
      yjlxList: [],
      //批示方式
      psfsList: [],
      //填写意见
      message: '',
      reviewType:'',//意见类型
      opiniontype:'',
      resultData:[],//字典获取所有意见类型
      jldReviewVal:'',
    };
  },
  mounted() {
    //默认值
    this.yjlx = ''
    this.psfs  = '签署意见'
    this.opiniontype = 'nibanReview'
    this.resultData = this.cache.get('reviewType')//意见类型的字典值数组
  },
  watch: {
    paramObject: {
      async handler(newValue, oldValue) {
        //如果子组件传值过来了，请求该方法
        if(this.util.isNotEmpty(newValue)){
          await this.getOpinionData(newValue.proInstId)
          await this.getCaacOpinion(newValue.proInstId)
        }
      },
      immediate: true,
      deep: true
    },
    showRecords(val){
      this.showCompo = val
    },
    psfs:{
      handler(nVal, oVal) {
        //如果是紧签名，则隐藏意见填写框
        if (nVal == '仅签名'){
          this.showOpinion = false
        }else{
          this.showOpinion = true
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    //意见类型
    yjlxChange(item) {
      //item {"name":"拟办意见","value":"nibanReview"}
      this.showyjlx = false;
      this.yjlx = item.name;
      this.opiniontype = item.value
      this.psfsList=[]
      if(item.value == 'nibanReview'){
        this.psfsList.push({'value':'2','name':"签署意见"})
        this.psfs='签署意见'
        // this.message = this.cache.get('userInfo').userName
      }else{
        this.psfsList.push({'value':'1','name':"仅签名"},{'value':'2','name':"签署意见"})
      }
    },

    //批示方式
    psfsChange(item) {
      //{"value":"2","name":"签署意见"}
      this.showpsfs = false;
      this.psfs = item.name;
      if(item.value == '1'){//仅签名
        this.message = this.cache.get('userInfo').userName
      }else{
        this.message=''
        this.getCaacOpinion( this.paramObject.proInstId)
      }
    },
    onSubmit() {
      this.saveopinion();
      this.$emit('closeRecords', this.showRecords);
    },
    toClose() {
      this.$emit('closeRecords', this.showRecords);
    },
    //获取意见类型
    async getOpinionData(proInstId){
      await this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/getOpinionData`),
        method: 'get',
        params: this.$http.adornParams({
          'proInstId': proInstId,
          'canGotoSpecialDraft':'true',
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        })
      }).then(({data}) => {
        if(data.code==200){
          if(this.util.isNotEmpty(data.data.opinion)){
            this.yjlxList=[]//清空数据
            data.data.opinion.forEach((item)=>{
              this.yjlxList.push({"name":item.dictItemName,"value":item.dictItemCode})
            })
            //选项默认值
            if(this.util.isNotEmpty(this.yjlxList)){
              this.yjlx = this.yjlxList[0].name;
              this.opiniontype = this.yjlxList[0].value
              this.psfsList=[]
              if(this.opiniontype== 'nibanReview'){
                this.psfsList.push({'value':'2','name':"签署意见"})
                this.psfs='签署意见'
                // this.message = this.cache.get('userInfo').userName
              }else{
                this.psfsList.push({'value':'1','name':"仅签名"},{'value':'2','name':"签署意见"})
              }
            }
          }
        }
      })
    },
    //获取意见,用来回显意见
    async getCaacOpinion(proInstId){
      let workItemId = this.paramObject.workitemId;
      await this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/approval/approvalwrite`),
        method: 'get',
        params: this.$http.adornParams({
          'proInstId': proInstId,
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        })
      }).then(({data}) => {
        if(data.code==200){
          Object.keys(data.data).forEach((item,index)=>{
            let subItem = data.data[item]
            if(subItem.length>0){
              for (let i = 0; i < subItem.length; i++) {
                //如果之前填写意见了那就在这个
                let approvalWorkItemId = subItem[i].workItemId;
                let approver = subItem[i].approver;
                let userId = this.cache.get('userInfo').userId
                if(approvalWorkItemId == workItemId && approver == userId ){
                  this.message=subItem[i].opinion;//意见回显
                  this.resultData.forEach((value)=>{
                    if(value.dictItemCode == item){
                      this.yjlx = value.dictItemName
                      this.opiniontype = value.dictItemCode
                    }
                  })
                }
              }
            }
          })
        }
      })
    },
    //填写意见接口
    saveopinion(){
      if(this.util.isEmpty(this.message)){
        this.message = this.cache.get('userInfo').userName
      }
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/approval/saveopinion`),
        method: 'post',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        }),
        data: this.$http.adornData({
          'proInstId': this.paramObject.proInstId,//发文详情返回的proInstId
          'workItemId': this.paramObject.workitemId,//
          'actInstId': this.paramObject.actInstId,//
          'opiniontype':this.opiniontype,
          'opinion':this.message,
          'isApproval':'1',//核稿状态，默认传1
        })
      }).then(({data}) => {
        if(data.code==200){
          this.$emit('refreshBtnGroup',true)//通知父组件重新加载数据
          this.$toast('意见填写成功')
        }else{
          this.$toast(data.message+' code:'+data.code)
        }

      })
    },

  },
};
</script>

<style scoped>
.css {
  text-align: center;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 3%;
}

.van-cell__title {
  min-width: 50%;

span {
  display: inline-block;
  text-align: left;
  word-break: break-all;
}
}

/deep/ .van-field__control {
  font-size: 14px;
  color: #969799;
}
</style>
